﻿using System;
using System.IO;

namespace System
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class FileHelper
    {
        #region 属性

        /// <summary>
        /// 文件路径
        /// </summary>
        /// <value>The file path.</value>
        private string FilePath { get; set; }

        private static FileHelper _LogCustom;
        /// <summary>
        /// Logger对象
        /// </summary>
        public static FileHelper LogCustom => _LogCustom ?? (_LogCustom = new FileHelper(nameof(LogCustom)));

        #endregion

        #region 构造
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="fileName"></param>
        public FileHelper(string fileName)
        {
            string folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log", fileName);
            FilePath = Path.Combine(folder, DateTimeHelper.Now_yyyyMMdd + ".txt");
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="message"></param>
        public void WriteLog(string message)
        {
            File.AppendAllText(FilePath,
                DateTimeHelper.Now_yyyy_MM_dd_HH_mm_ss +
                "  " +
                message +
                Environment.NewLine);
        }
        #endregion
    }
}